package com.aibaixun.web.controller.system;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.Tree;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aibaixun.common.core.domain.R;
import com.aibaixun.common.exception.ServiceException;
import com.aibaixun.system.domain.DicmAddress;
import com.aibaixun.system.domain.DicmNvr;
import com.aibaixun.system.domain.DicmSignalh;
import com.aibaixun.system.domain.PostureDevice;
import com.aibaixun.system.domain.dto.*;
import com.aibaixun.system.domain.vo.*;
import com.aibaixun.system.service.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * dicm基础数据信息管理
 *
 * @author zhangyh
 * @date 2023-09-01
 **/
@RestController
@RequestMapping("/dicm/data" )
public class DicmBaseDataController {
    private static final Logger logger = LoggerFactory.getLogger(DicmBaseDataController.class);

    @Autowired
    private DicmWorkOrderService dicmWorkOrderService;

    @Autowired
    private DicmAddressService dicmAddressService;

    @Autowired
    private DicmSignalhService dicmSignalhService;

    @Autowired
    private DicmNvrService dicmNvrService;

    @Autowired
    private PostureDeviceService postureDeviceService;

    @Autowired
    private EvsApiService evsApiService;

    /**
     * 获取地址树
     * @return
     */
    @ApiOperation(value = "获取地址树")
    @PostMapping("/address/tree")
    public R<List<Tree<String>>> getTree(@RequestBody DicmAddress dto){
        return dicmAddressService.getTree(dto);
    }

    @ApiOperation("根据房间ID查询设备树")
    @GetMapping("/query/deviceByRoomId")
    public R queryDeviceByRoomId(@RequestParam String parentId){
        return dicmAddressService.queryDeviceByRoomId(parentId);
    }

    @ApiOperation("根据ueId查询设备详情")
    @GetMapping("/query/byCabinetId")
    public R queryByRoomId(@RequestParam String cabinetId) throws UnsupportedEncodingException {
        cabinetId = URLDecoder.decode(cabinetId, "UTF-8");
        QueryDeviceVo vo = dicmAddressService.queryByRoomId(cabinetId);
        return R.ok(vo);
    }

    /**
     * 分页查询摄像头列表
     * @return
     */
    @ApiOperation(value = "分页查询摄像头列表")
    @PostMapping("/nvr/page")
    public R<Page<DicmNvr>> getPage(@RequestBody DicmNvrPageDto pageDto){
        Page page = new Page();
        page.setCurrent(pageDto.getCurrent());
        page.setSize(pageDto.getSize());
        page.setSearchCount(true);
        return dicmNvrService.getPage(page, pageDto);
    }

    /**
     * 查询摄像头详情
     * @return
     */
    @ApiOperation(value = "查询摄像头详情")
    @GetMapping("/nvr/detail/{nvrId}")
    public R<DicmNvr> getNvrDetail(@PathVariable(value = "nvrId") String nvrId){
        return dicmNvrService.getNvrDetail(nvrId);
    }

    /**
     * 根据设备ID获取设备性能监控信息
     * @return
     */
    @ApiOperation(value = "根据设备ID获取设备性能监控信息")
    @GetMapping("/device/signalh/{deviceId}")
    public R<List<DicmSignalh>> getDicmSignalhList(@PathVariable(value = "deviceId") String deviceId){
        return dicmSignalhService.getDicmSignalhList(deviceId);
    }

    /**
     * 根据ueid获取设备性能监控信息
     */
    @GetMapping("/get/deviceSignalh/{ueId}")
    public R<List<DicmSignalh>> getDeviceSignalh(@PathVariable(value = "ueId") String ueId){
        return dicmSignalhService.getDeviceSignalh(ueId);
    }

    /**
     * 获取所有机柜已用容量信息
     * @return
     */
    @ApiOperation(value = "获取所有机柜已用容量信息")
    @GetMapping("/cabinet/space")
    public R getCabinetSpace(){
        return postureDeviceService.getCabinetSpace();
    }

    /**
     * 获取云图性能数据
     * @return
     */
    @ApiOperation(value = "获取云图性能数据")
    @PostMapping("/device/energy/list")
    public R<List<Map<String, String>>> getDeviceEnergyList(@RequestBody DicmSignalhEnergyDto dto){
        return dicmSignalhService.getDeviceEnergyList(dto);
    }

    /**
     * 获取云图性能数据（ue）
     * @return
     */
    @ApiOperation(value = "获取云图性能数据（ue）")
    @PostMapping("/device/energy/list/ue")
    public R<Map<String, String>> getDeviceEnergyListUe(@RequestBody DicmSignalhEnergyDto dto){
        return dicmSignalhService.getDeviceEnergyListUe(dto);
    }

    /**
     * 工单信息推送
     * @return
     */
    @ApiOperation(value = "工单信息推送")
    @PostMapping("/workOrder/push")
    public R workOrderPush(@RequestBody @Validated DicmWorkOrderDto dto){
        return dicmWorkOrderService.workOrderPush(dto);
    }

    /**
     * 根据位置ID绑定ue信息
     * @param dto
     * @return
     */
    @PostMapping("/bind/deviceUe")
    public R bindDeviceUe(@RequestBody BindDeviceDto dto){
       return dicmAddressService.bindDeviceUe(dto);
    }

    @GetMapping("/test/registerNvr")
    public R testRegisterNvr(){
        evsApiService.getEvsDeviceId();
        return R.ok();
    }

    @GetMapping("/unbind/all")
    public R unbindAll(){
        evsApiService.unbindAll();
        return R.ok();
    }

    @PostMapping("/get/dicmAddressMessage")
    public R getDicmAddressMessage(@RequestBody JSONObject resultData){
        evsApiService.getAssetsMessage1(resultData);
        return null;
    }
    @GetMapping("/query/deviceMessage")
    public void queryDeviceMessage(){
        evsApiService.getAssetsMessage();
//        List<QueryPostureDeviceDto> postureList = JSONUtil.toList(JSONUtil.parseArray(cabinetJson.get("data")), QueryPostureDeviceDto.class);
//        evsApiService.savePostureDevice(postureList);
    }

    @GetMapping("/reset/ueId")
    public void resetUeId(){
        dicmAddressService.resetUeId();
    }


}
